/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月20日上午7:07:49
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * ThreadLocal类及应用技巧
 * @author David
 * @since 2015年8月20日 上午7:07:49
 * @version 
 * @since JDK 1.6
 */
public class ThreadLocalDemo {
	
	private static ThreadLocal<Integer> x=new ThreadLocal<Integer>();
	private static ThreadLocal<MyThreadScopeData> myThreadScopeData
				   =new ThreadLocal<MyThreadScopeData>();
	
	private static Map<Thread,Integer> threadData=new HashMap<Thread,Integer>();
	public static void  main(String[] args) {
		for(int i=0;i<2;i++){
			new Thread(new Runnable() {
				@Override
				public void run() {
					int data=new Random().nextInt();
					System.out.println(Thread.currentThread().getName()
							+" has put data:"+data);
					x.set(data);
//					MyThreadScopeData myData=new MyThreadScopeData();
//					myData.setName("name "+data);
//					myData.setAge(data);
//					myThreadScopeData.set(myData);
					MyThreadScopeData.getThreadInstance().setName(""+data);
					MyThreadScopeData.getThreadInstance().setAge(""+data);
					new A().get();
					new B().get();
				}
			}).start();
		} 
	}

	static class A{
		public void get(){
//			int data=x.get();
//			System.out.println("A from "+Thread.currentThread().getName()
//					+" get data:"+data);
			/*MyThreadScopeData myData=myThreadScopeData.get();
			System.out.println("A from "+Thread.currentThread().getName()
					+" get name:"+myData.getName()+" age:"+myData.getAge());
		*/
			
			MyThreadScopeData myData=MyThreadScopeData.getThreadInstance();
			System.out.println("A from "+Thread.currentThread().getName()
					+" get name:"+myData.getName()+" age:"+myData.getAge());
			
			
			
		}
	}
	
	static class B{
		public void get(){
			int data=x.get();
//			System.out.println("B from "+Thread.currentThread().getName()
//					+" get data:"+data);
			
			MyThreadScopeData myData=MyThreadScopeData.getThreadInstance();
			System.out.println("B from "+Thread.currentThread().getName()
					+" get name:"+myData.getName()+" age:"+myData.getAge());
			
		
		}
	}
}

class MyThreadScopeData{
	private MyThreadScopeData(){}
	
	public static /*synchronized*/ MyThreadScopeData getThreadInstance(){
		MyThreadScopeData instance=map.get();
		if(instance==null){
			instance=new MyThreadScopeData();
			map.set(instance);
		}
		return instance;
	}
	
//	private static MyThreadScopeData instance=null;
	private static ThreadLocal<MyThreadScopeData> map=
			       new ThreadLocal<MyThreadScopeData>();
	
	
	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
}



















